﻿Imports ISNet.WebUI.WebGrid
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports log4net

Partial Class ProjectDataCenter
    Inherits System.Web.UI.Page

    Public ScripText As String = ""
    Private Shared logger As ILog = LogManager.GetLogger("ProjectDataCenter")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        ScreenName.Text = "ระบบจัดทำงบประมาณ  >>  จัดทำงบประมาณ >> ข้อมูลหลักโครงการ / งานสนับสนุน"
        'Session("users") = "bb0011"
        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        If IsPostBack = False Then
            PlanYearsTxt.Text = Session("PlanYear")
            Session("PlanYearTxt") = PlanYearsTxt.Text
            Session("searchtext") = ""
            'Else
            '    PlanYearsTxt.Text = Session("PlanYearTxt")
        End If
    End Sub

    Protected Sub WebGrid1_InitializeDataSource(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.DataSourceEventArgs)
        Dim adapter As New B10DataSetTableAdapters.ProjectsTableAdapter
        Dim dtTable As New B10DataSet.ProjectsDataTable
        Dim sql As String = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        If Session("searchtext") = "" And Session("savedata") = "" Then
            e.DataSource = dtTable
            Exit Sub
        ElseIf Session("savedata") <> "" Then
            Session("searchtext") = ""
            Session("searchtext") += " and PlanYear = " & Val(Session("PlanYearTxt"))
            If WebCombo3.Value <> "" Then
                Session("searchtext") += " and  FundCenter_FundCenterCode  = '" & WebCombo3.Value & "'"
            Else
                Session("searchtext") += " and  FundCenter_FundCenterCode  = '" & Session("savedata") & "'"
            End If

            Session.Remove("savedata")
        End If
        'If IsPostBack = True And Session("Search") = True Then

        sql = "select * from Projects "
        sql += " WHERE 1 = 1 "
        sql += Session("searchtext")

        sql += " ORDER BY ProjectCode "
        'Response.Write(sql)
        adapter.GetDataByFilter(dtTable, sql)
        e.DataSource = dtTable
        Session("count_data") = dtTable.Rows.Count

        dtTable.Columns("PlanYear").AllowDBNull = True
        dtTable.Columns("ProjectCode").AllowDBNull = True
        dtTable.Columns("Description").AllowDBNull = True

        'Else

        'sql = "select * from Projects "
        'sql += " WHERE 1 = 1 "
        'sql += " and PlanYear like '" & Val(Session("PlanYearTxt")) & "'"

        'If prefil <> "" Then
        '    sql += " AND FundCenter_FundCenterCode IN (" & prefil & ") "
        'End If

        'sql += " ORDER BY ProjectCode "

        'adapter.GetDataByFilter(dtTable, sql)

        'e.DataSource = dtTable
        'Session("count_data") = dtTable.Rows.Count

        'dtTable.Columns("PlanYear").AllowDBNull = True
        'dtTable.Columns("ProjectCode").AllowDBNull = True
        'dtTable.Columns("Description").AllowDBNull = True


        'End If
        ' Response.Write(sql)

    End Sub

    Protected Sub WebGrid1_InitializeRow(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.RowEventArgs)
        ' don't edit column
        e.Row.Cells(0).ForceNoEdit = True
        e.Row.Cells(1).ForceNoEdit = True
        e.Row.Cells(2).ForceNoEdit = True

    End Sub

    Protected Sub WebGrid1_InitializePostBack(ByVal sender As Object, ByVal e As ISNet.WebUI.WebGrid.PostbackEventArgs)
        If e.Action = PostBackAction.Custom Then
            Dim Deletet As String = TryCast(Request("id"), String)
            'Dim id = Deletet.Split(",")
            'Dim idd = id(0)
            'Dim idy = id(1)

            Dim adapter As New B10DataSetTableAdapters.ProjectsTableAdapter
            ' MsgBox(Deletet)
            Dim dt As New B10DataSet.ProjectsDataTable
            dt = adapter.GetDataByID(Deletet)
            Dim adap2 As New B10DataSetTableAdapters.BudgetDetailsTableAdapter
            If dt.Count > 0 Then
                'WebGrid1.ClientAction.Alert(dt.Item(0).ProjectCode & " " & dt.Item(0).PlanYear & " " & dt.Item(0).FundCenterCode)
                Dim counterx As Decimal = 0

                If dt.Item(0).IsFundCenter_FundCenterCodeNull Then
                    counterx = adap2.CheckGlobalExist(dt.Item(0).ProjectCode, dt.Item(0).PlanYear)
                Else
                    counterx = adap2.CheckPrjExist(dt.Item(0).ProjectCode, dt.Item(0).PlanYear, dt.Item(0).FundCenterCode)
                End If

                If counterx > 0 Then
                    WebGrid1.ClientAction.Alert("ไม่สามารถลบได้เนื่องจากข้อมูลถูกนำไปใช้แล้ว")
                    Exit Sub
                End If
            End If

            Dim adap3 As New B10DataSetTableAdapters.ProjectFundCentersTableAdapter
            Dim idProject As Decimal = Val(Request("id"))
            adap3.DeleteFromProjects(idProject)
            adapter.DeleteQuery(Deletet)

            logger.Info("User " & Session("users") & " delete project code " & Deletet)

            ScripText = ""
            WebGrid1.ClientAction.Refresh()
        End If
    End Sub

    Protected Sub SearhBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SearhBtn.Click
        If WebCombo3.Value = "" Then
            ScripText = "<script> alert('กรุณาเลือก Fundcenter');</script>"
            Exit Sub
        End If
        Session("PlanYearTxt") = Val(PlanYearsTxt.Text)
        Session("searchtext") = ""

        Session("searchtext") += " and PlanYear = " & Val(Session("PlanYearTxt"))

        If WebCombo3.Value <> "" Then
            If WebCombo3.Value <> "0" Then
                Session("searchtext") += " and  FundCenter_FundCenterCode  = '" & WebCombo3.Value & "'"
            Else
                Session("searchtext") += " and  FundCenter_FundCenterCode  is null "
            End If
        Else

        End If

        Session.Remove("AddFundCenterToProjectIcon")
        If WebCombo3.Value <> "" Then
            If WebCombo3.Value = "0" Then
                Session.Add("AddFundCenterToProjectIcon", 1)
            End If
        End If
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
        Session.Remove("Search")
        Session.Add("Search", True)
    End Sub

    Protected Sub WebCombo3_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo3.InitializeDataSource
        Dim adap As New BasicDataTableAdapters.FCTR_ChildTableAdapter
        Dim dt As New BasicData.FCTR_ChildDataTable
        Dim sql = ""
        Dim prefil As String = ""
        prefil = Session("prefilter")
        sql += " WHERE 1 = 1 "
        sql += " AND FundCenters.PlanYear = " & Val(Session("PlanYearTxt")) & " AND FundCenterHierarchies.PlanYear = " & Val(Session("PlanYearTxt"))
        sql += " AND FundCenterHierarchies.FundCenterGroup = 1 "
        sql += " and FundCenterHierarchies.PlanYear = " & Val(Session("PlanYear")) & " "
        sql += " ORDER BY FundCenterCode ASC"
        adap.GetDataByFilter(dt, sql)

        dt.Rows.Add("0", "โครงการกลาง", "1")

        dt.DefaultView.Sort = "FundCenterCode"
        e.DataSource = dt

        If dt.Count = 1 Then
            WebCombo3.Value = dt.Item(0).FundCenterCode
        End If
    End Sub

    Protected Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs) Handles ImageButton1.Click
        ScripText = "<script> popupkrajay3('" & 1 & "','" & PlanYearsTxt.Text & "','" & 0 & "');</script>"
    End Sub

    Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
        WebGrid1.ClearCachedDataSource()
        WebGrid1.RebindDataSource()
    End Sub
End Class
